package com.daxt.utils;

import com.daxt.model.xml.oa.BaseInfoXml;
import com.daxt.model.xml.oa.ProcessXml;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import java.io.*;

public class XmlUtil {

    /**
     * 将XML转为指定的POJO对象
     *
     * @param clazz 需要转换的类
     * @param xmlStr xml数据
     * @return
     */
    public static Object xmlStrToObject(Class<?> clazz, String xmlStr) throws Exception {
        Object xmlObject = null;
        Reader reader = null;
        //利用JAXBContext将类转为一个实例
        JAXBContext context = JAXBContext.newInstance(clazz);
        //XMl 转为对象的接口
        Unmarshaller unmarshaller = context.createUnmarshaller();
        reader = new StringReader(xmlStr);
        xmlObject = unmarshaller.unmarshal(reader);
        if (reader != null) {
            reader.close();
        }
        return xmlObject;
    }




    /*public static void main(String[] args0){
        //读取Resource目录下的XML文件
//        Resource resource = new ClassPathResource("cityList.xml");

        File xml1 = new File("D:\\a\\zipPage\\办理流程.xml");
        ProcessXml xml = new ProcessXml();

        ProcessXml baseInfo = (ProcessXml) parseOaPage(xml1,xml);
        System.err.println("-------------");
//        File xml1 = new File("D:\\a\\zipPage\\基本信息.xml");
//        BaseInfoXml biXml = new BaseInfoXml();
//
//        BaseInfoXml baseInfo = (BaseInfoXml) parseOaPage(xml1,biXml);
//        System.err.println("-------------");

//        try {
//            InputStream inputStream = new FileInputStream(xml1);
//            //利用输入流获取XML文件内容
//            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
//            StringBuffer buffer = new StringBuffer();
//            String line = "";
//            while ((line = br.readLine()) != null) {
//                buffer.append(line);
//            }
//            br.close();
//
//            //XML转为JAVA对象
//            BaseInfoXml baseInfo = (BaseInfoXml) xmlStrToObject(BaseInfoXml.class, buffer.toString());
//            System.err.println("22222222:"+baseInfo.getFilingId());
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }



    }*/


    public static Object parseOaPage(File xmlFile, Object bean){
        try {
            InputStream inputStream = new FileInputStream(xmlFile);
            //利用输入流获取XML文件内容
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
//            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            StringBuffer buffer = new StringBuffer();
            String line = "";
            while ((line = br.readLine()) != null) {
                buffer.append(line);
            }
            br.close();

            //XML转为JAVA对象
            bean = xmlStrToObject(bean.getClass(), buffer.toString());
//            System.err.println("22222222:"+baseInfo.getFilingId());
//            BaseInfoXml baseInfo = (BaseInfoXml) xmlStrToObject(BaseInfoXml.class, buffer.toString());
//            System.err.println("22222222:"+baseInfo.getFilingId());

            return bean;

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;

    }
//    public static Object parseOaPage(File xmlFile, Object bean){
//        try {
//            InputStream inputStream = new FileInputStream(xmlFile);
//            //利用输入流获取XML文件内容
//            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
//            StringBuffer buffer = new StringBuffer();
//            String line = "";
//            while ((line = br.readLine()) != null) {
//                buffer.append(line);
//            }
//            br.close();
//
//            //XML转为JAVA对象
//            Object asds = xmlStrToObject(bean.getClass(), buffer.toString());
////            System.err.println("22222222:"+baseInfo.getFilingId());
////            BaseInfoXml baseInfo = (BaseInfoXml) xmlStrToObject(BaseInfoXml.class, buffer.toString());
////            System.err.println("22222222:"+baseInfo.getFilingId());
//
//            return asds;
//
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//        return null;
//
//    }


}
